package com.app.micaihu.view.home.questions.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.comment.NormalComment;
import com.app.micaihu.bean.comment.PariseBean;
import com.app.micaihu.bean.speak.IAttUser;
import com.app.micaihu.bean.topic.PostDetailEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.view.AttentionView;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.custom.view.MyGridView;
import com.app.micaihu.listener.AutoWidthControllerListener;
import com.app.micaihu.listener.HandleListener;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.AppShareUtils;
import com.app.micaihu.utils.AppViewUtils;
import com.app.micaihu.utils.CommentUtils1;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.ImageLoadUtils;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.micaihu.utils.StatServiceUtils;
import com.app.micaihu.view.army.activity.ArmyDetailActivity;
import com.app.micaihu.view.army.activity.ArmyMedalListActivity;
import com.app.micaihu.view.comment.SubmitCommentListener;
import com.app.micaihu.view.comment.adapter.NormalCommentAdapter;
import com.app.micaihu.view.main.topic.adapter.SendImageGridAdapter;
import com.app.micaihu.view.newsdetail.CommentPariseActivity;
import com.app.micaihu.view.newsdetail.ImageShowActivity;
import com.app.micaihu.view.newsdetail.VideoDetailActivity;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.utils.pulltorefresh.PullToRefreshBase;
import com.app.utils.pulltorefresh.PullToRefreshListView;
import com.app.utils.util.StringUtils;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.osutils.StatusBarUtils;
import com.app.utils.util.view.expression.view.BiaoQinTextView;
import com.app.widget.LoadView;
import com.blankj.utilcode.util.ActivityUtils;
import com.google.gson.reflect.TypeToken;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Guo on 2016/12/21.
 * <p>
 * <p>
 * 帖子详情--204新
 */

public class QuestionDetailActivity extends BaseOldActivity implements AppShareUtils.DoMoreClickListener, View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener, NormalCommentAdapter.OnItemOpratedListener {
    public final int NO_NET = 0;// 网络错误
    public final int DATAERROR = 1;// 数据错误
    public final int LOADING = 2;// 数据加载中
    public final int LOADSUCES = 3;// 加载成功
    public final int DATAEMPTY = 4;// 数据为空
    // 数据变量
    protected List<NormalComment> dataList;// 数据对象集合
    protected AppBaseAdapter<NormalComment> mAdapter;// 数据适配器

    private String commentId;
    private ImageView ivAppRight;
    private CustomImageView civHeadpic;
    private TextView tvNickname;
    private TextView tvPublishtime;
    private BiaoQinTextView tvPostcontent;
    private FrameLayout flAllImgContainer;

    private CustomImageView civThumbBig;
    private CustomImageView civRank;
    private TextView tvRankName;
    private AttentionView attView;

    private ImageView ivHonourPic;
    private int addCommentIndex;
    private boolean noNormal = true;
    private List<String> imgList;
    private int currentClickedIndex = 0;
    private SubmitCommentListener commentListener;
    private PostDetailEntity postDetailEntity;


    public static final String DELETE_CODE = "delete_code";
    private AppShareUtils mShareUtils;
    private Bundle bundle;
    private View headerView;
    private PullToRefreshListView mListView;
    private LoadView mLoadView;
    protected int dataPage = 1;// 请求的数据页数
    private boolean seeComment = false;
    private MyGridView gridView;
    private TextView tvPostTitle, tvArmyGroupInfo;
    private List<PariseBean> praiseList;


    public static final String COMMENT_SUM = "COMMENT_SUM";//列表页中的评论数
    public static final String PARISE_SUM = "PARISE_SUM";//列表页中的点赞数
    private TextView mark;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        isInit = false;
        Intent intent = getIntent();
        if (intent != null) {
            commentId = intent.getStringExtra(Config.Extra.PARAMETER1);
            seeComment = intent.getBooleanExtra("seeComment", false);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_detail_activity);
        StatusBarUtils.setupStatusBarView(this, R.color.common_bg_color_2, R.color.common_bg_color_10);
        StatusBarUtils.setStatusBarTextColor(this, true);
        initView();
        loadData(true);
    }


    private void initView() {
        setTitleRightImage(R.drawable.detail_menu_black_selector);

        ivAppRight = (ImageView) findViewById(R.id.page_head_function);
        if (ivAppRight != null) ivAppRight.setOnClickListener(this);
        findViewById(R.id.page_head_back).setVisibility(View.VISIBLE);
        ((ImageView) findViewById(R.id.page_head_back)).setImageResource(R.drawable.common_back_selector);

        mListView = (PullToRefreshListView) findViewById(R.id.prlv_listview);
        mLoadView = (LoadView) findViewById(R.id.ld_loadview);
        // Toast提示框
        TextView notify_view_text = (TextView) findViewById(R.id.tv_notify);
        notify_view_text.setVisibility(View.GONE);
        mLoadView.setErrorPageClickListener(this);

        // 配置mListView
        AppViewUtils.initPullToRefreshListView(mListView);
        mListView.setEmptyView(mLoadView);
        mListView.setOnRefreshListener(this);


        //设置底部评论按钮
        setBottom();
        //设置header 帖子主体
        initHeaderView();
    }

    /**
     * 设置底部
     */
    private void setBottom() {
        FrameLayout flCommonBottom = findViewById(R.id.fl_common_bottom);
        if (flCommonBottom != null) {
            View view = this.getLayoutInflater().inflate(R.layout.layout_postsend_bottom, null);
            TextView tvComment = view.findViewById(R.id.tv_imagecomment);
            tvComment.setText("写回答提升军衔");
            flCommonBottom.addView(view);
            flCommonBottom.setOnClickListener(this);
        }
    }


    /**
     * 初始化listview 的header
     * <p>
     * 作为帖子
     */
    private void initHeaderView() {
        headerView = this.getLayoutInflater().inflate(R.layout.question_item_detail_header, null);
        civHeadpic = (CustomImageView) headerView.findViewById(R.id.civ_headpic);
        headerView.findViewById(R.id.llComment).setOnClickListener(this);
        tvArmyGroupInfo = (TextView) headerView.findViewById(R.id.tvArmyGroupInfo);
        tvNickname = (TextView) headerView.findViewById(R.id.tv_nickname);
        tvPublishtime = (TextView) headerView.findViewById(R.id.tv_publishtime);
        gridView = (MyGridView) headerView.findViewById(R.id.gridview);
        tvPostTitle = (TextView) headerView.findViewById(R.id.tv_title);

        gridView.setOnItemClickListener(this);
        tvPostcontent = (BiaoQinTextView) headerView.findViewById(R.id.tv_postcontent);
        tvPostcontent.setMaxLines(Integer.MAX_VALUE);//对帖子的内容取消5行的限制

        TextView tvAllcontent = (TextView) headerView.findViewById(R.id.tv_allcontent);
        tvAllcontent.setVisibility(View.GONE);//将“全文”隐藏掉

        flAllImgContainer = (FrameLayout) headerView.findViewById(R.id.fl_imagecontainer);

        civThumbBig = (CustomImageView) headerView.findViewById(R.id.civ_big_thumb);
        civRank = (CustomImageView) headerView.findViewById(R.id.civ_rank);
        tvRankName = (TextView) headerView.findViewById(R.id.tv_rankname);
        attView = (AttentionView) headerView.findViewById(R.id.tv_attention);
        ivHonourPic = (ImageView) headerView.findViewById(R.id.ivHonourPic);

        civHeadpic.setOnClickListener(this);
        tvNickname.setOnClickListener(this);

        //为图片加点击事件
        civThumbBig.setOnClickListener(this);

        mListView.getRefreshableView().addHeaderView(headerView);

        mark = (TextView) findViewById(R.id.tv_num);
        mark.setVisibility(View.GONE);


    }


    private void loadData(final boolean isRefresh) {
        Map<String, String> params = new HashMap<>();
        params.put("commentId", commentId);

        if (UserInforManager.getInstance().isLogin()) {
            params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        } else {
            params.put("uid", "");
        }

        params.put("page", dataPage + "");


        final HttpListener<DataBean<PostDetailEntity>> postDetailListener = new HttpListener<DataBean<PostDetailEntity>>() {
            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override
            public void onSuccess(DataBean<PostDetailEntity> response) {
                if (!response.noError()) {
                    setLoadView(DATAERROR, response.getMsg());

                } else {
                    postDetailEntity = response.getData();
                    List<NormalComment> list = null;
                    if (postDetailEntity != null) {

                        //设置帖子数据
                        setHeaderData(postDetailEntity);
                        if (postDetailEntity.getHotCmtList() != null && postDetailEntity.getHotCmtList().size() > 0) {
                            for (int i = 0; i < postDetailEntity.getHotCmtList().size(); i++) {
                                if (InteractiveUtils.getInstance().theDefriendIsContains(postDetailEntity.getHotCmtList().get(i).getUid())) {
                                    postDetailEntity.getHotCmtList().remove(postDetailEntity.getHotCmtList().get(i));
                                    i--;
                                }
                            }
                        }
                        if (postDetailEntity.getNormalList() != null && postDetailEntity.getNormalList().size() > 0) {
                            for (int i = 0; i < postDetailEntity.getNormalList().size(); i++) {
                                if (InteractiveUtils.getInstance().theDefriendIsContains(postDetailEntity.getNormalList().get(i).getUid())) {
                                    postDetailEntity.getNormalList().remove(postDetailEntity.getNormalList().get(i));
                                    i--;
                                }
                            }
                        }


                        if ((postDetailEntity.getHotCmtList() != null && postDetailEntity.getHotCmtList().size() > 0)
                                || (postDetailEntity.getNormalList() != null && postDetailEntity.getNormalList().size() > 0)) {
                            if (dataList == null) {
                                dataList = new ArrayList<NormalComment>();
                            }
                            if (mAdapter == null) {
                                mAdapter = new NormalCommentAdapter(dataList, mActivity, false);
                                if (mAdapter instanceof NormalCommentAdapter) {
                                    ((NormalCommentAdapter) mAdapter).setOnItemOpration(QuestionDetailActivity.this);
                                }
                                mListView.setAdapter(mAdapter);
                            }
                            if (addCommentIndex == 0) {
                                //先判断热门,因为现在可能只给热门,不给normal
                                if (postDetailEntity.getHotCmtList() != null && postDetailEntity.getHotCmtList().size() > 0) {
                                    addCommentIndex = postDetailEntity.getHotCmtList().size() + 2;
                                    ((NormalCommentAdapter) mAdapter).currentLastHotIndex = postDetailEntity.getHotCmtList().size();
                                } else {
                                    addCommentIndex = 1;
                                }
                                list = postDetailEntity.getAllQuestionList();
                            } else {
                                list = postDetailEntity.getNormalList();
                            }
                            if (postDetailEntity.getNormalList() != null) {
                                if (postDetailEntity.getNormalList().size() <= 0) {
                                    noNormal = true;
                                } else {
                                    noNormal = false;
                                }
                            }
                            if (isRefresh) {
                                //清空数据
                                dataList.clear();
                            }
                            dataList.addAll(list);
                            if (mAdapter == null) {
                                mAdapter = new NormalCommentAdapter(dataList, mActivity, false);
                                if (mAdapter instanceof NormalCommentAdapter) {
                                    ((NormalCommentAdapter) mAdapter).setOnItemOpration(QuestionDetailActivity.this);
                                }
                                mListView.setAdapter(mAdapter);
                            } else {
                                mAdapter.notifyDataSetChanged();
                            }


                            setLoadView(LOADSUCES, null);
                            if (isRefresh && seeComment) {
                                switchItem();
                                seeComment = false;
                            }
                        } else {
                            if (isRefresh) {
                                if (dataList == null) {
                                    dataList = new ArrayList<NormalComment>();
                                }
                                if (mAdapter == null) {

                                    if (dataList.size() == 0) {
                                        dataList.add(null);
                                    }


                                    mAdapter = new NormalCommentAdapter(dataList, mActivity, false);
                                    if (mAdapter instanceof NormalCommentAdapter) {
                                        ((NormalCommentAdapter) mAdapter).setOnItemOpration(QuestionDetailActivity.this);
                                    }
                                    mListView.setAdapter(mAdapter);
                                } else {
                                    mAdapter.notifyDataSetChanged();
                                }
                            }
                            if (dataList != null && dataList.size() > 0) {
                                setLoadView(LOADSUCES, getResources().getString(R.string.nomore_comment));
                            }
                        }
                    } else {
                        setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
                    }

                }
            }
        };
        addPostCacheRequest(URIContant.TOPIC_POST_DETAIL_2052, new TypeToken<DataBean<PostDetailEntity>>() {
        }.getType(), params, postDetailListener);


    }


    private void setHeaderData(PostDetailEntity postDetailEntity) {
        LoadImageUtils.getInstance().loadCircleImage(civHeadpic, postDetailEntity.getHeadPic());
        tvNickname.setText(postDetailEntity.getNickName());
        tvPublishtime.setText(postDetailEntity.getPublishTime());
        LoadImageUtils.getInstance().loadImage(civRank, postDetailEntity.getRankIcon());
        tvArmyGroupInfo.setText(postDetailEntity.getArmyGroupInfo());
        ImageLoadUtils.loadImage(postDetailEntity.getHonourPic(), ivHonourPic);
        ivHonourPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!com.blankj.utilcode.util.StringUtils.isEmpty(postDetailEntity.getUid())) {
                    Intent intent = new Intent(QuestionDetailActivity.this, ArmyMedalListActivity.class);
                    intent.putExtra(Config.Extra.toUid, postDetailEntity.getUid());
                    ActivityUtils.startActivity(intent);
                }
            }
        });
        tvArmyGroupInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!com.blankj.utilcode.util.StringUtils.isEmpty(postDetailEntity.getUid()) && !com.blankj.utilcode.util.StringUtils.isEmpty(postDetailEntity.getGid())) {
                    Intent intent = new Intent(QuestionDetailActivity.this, ArmyDetailActivity.class);
                    intent.putExtra(Config.Extra.toUid, postDetailEntity.getUid());
                    intent.putExtra(Config.Extra.id, postDetailEntity.getGid());
                    ActivityUtils.startActivity(intent);
                }
            }
        });
        ImageLoadUtils.loadImage(postDetailEntity.getRankIcon(), civRank);
        tvRankName.setText(postDetailEntity.getRankName());
        //刷新关注按钮
        attView.setTag(postDetailEntity);
        attView.setSucessListener(sucessListener);

        if (TextUtils.isEmpty(postDetailEntity.getTitle())) {
            tvPostTitle.setVisibility(View.GONE);
        } else {
            tvPostTitle.setVisibility(View.VISIBLE);
            tvPostTitle.setText(postDetailEntity.getTitle());
        }
        //保存当前关注状态
//        iAttType = postDetailEntity.getIAttType();


        if (TextUtils.isEmpty(postDetailEntity.getContent())) {
            tvPostcontent.setVisibility(View.GONE);
        } else {
            tvPostcontent.setVisibility(View.VISIBLE);
            tvPostcontent.setPicText(postDetailEntity.getContent());

        }

        imgList = postDetailEntity.getImgList();

        setThumbData();
    }


    private AttentionView.SucessListener sucessListener = new AttentionView.SucessListener() {
        @Override
        public void changeView(IAttUser iAttUser) {
            if (iAttUser != null) {
                Bundle bundleAtt = new Bundle();
                bundleAtt.putInt("type", 1);
                bundleAtt.putString(Config.Extra.PARAMETER1, iAttUser.getIAttType());
                bundleAtt.putString(Config.Extra.PARAMETER2, iAttUser.getIToUid());
                EventBus.getDefault().post(bundleAtt);

            }


        }
    };


    /**
     * 设置图片的
     */
    private void setThumbData() {
        if (imgList == null || imgList.size() <= 0) {
            flAllImgContainer.setVisibility(View.GONE);
        } else {
            flAllImgContainer.setVisibility(View.VISIBLE);
            if (imgList.size() == 1) {
                civThumbBig.setVisibility(View.VISIBLE);
                gridView.setVisibility(View.GONE);
                AutoWidthControllerListener autoWidthControllerListener = new AutoWidthControllerListener(civThumbBig);
                AutoWidthControllerListener.OnLoadCompeleted onLoadCompeleted = new AutoWidthControllerListener.OnLoadCompeleted() {
                    @Override
                    public void onLoadCompeleted(CustomImageView civ, boolean flag) {
                        if (flag && mark != null) {
                            mark.setVisibility(View.VISIBLE);
                            mark.setText(AppApplication.getInstance().getString(R.string.longpic));
                        } else {
                            mark.setVisibility(View.GONE);
                        }
                    }
                };
                autoWidthControllerListener.setOnLoadCompeleted(onLoadCompeleted);

                LoadImageUtils.getInstance().loadImage(civThumbBig, imgList.get(0), autoWidthControllerListener);
            } else {
                civThumbBig.setVisibility(View.GONE);
                gridView.setVisibility(View.VISIBLE);
                SendImageGridAdapter sendImageGridAdapter = new SendImageGridAdapter(imgList, this, 30f);
                gridView.setAdapter(sendImageGridAdapter);


            }
        }


    }

    /**
     * 展示图片大图
     */
    private void showThumb() {
        Intent intent = new Intent(this, ImageShowActivity.class);
        intent.putStringArrayListExtra(Config.Extra.PARAMETER1, (ArrayList<String>) imgList);
        intent.putExtra(Config.Extra.PARAMETER2, currentClickedIndex);
        intent.setClass(this, ImageShowActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        overridePendingTransition(R.anim.base_alpha_in, R.anim.base_slide_remain);// 前一个参数是下一个Activity的效果


    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.page_head_function://右上角更多
                share();
                break;
            case R.id.error_page:
                loadData(true);
                break;
            case R.id.civ_headpic://跳转个人主页
            case R.id.tv_nickname:
                if (postDetailEntity != null) {
                    MyHomepageDynamicActivity.goToUserHomePage(this, postDetailEntity.getUid());

                }
                break;
            case R.id.fl_laud://点赞
//                InteractiveUtils.getInstance().laud((FrameLayout) view, laudListener);
//                StatService.onEvent(this, "047", "帖子点赞", 1);// 统计的点击
                break;
            case R.id.llComment://评论
//                if ("1".equals(indexType)) {
//                    StatService.onEvent(this, "post_comment", "推荐", 1);// 统计的点击的次数
//                    MobclickAgentUtils.event(Config.EventId.post_comment, "推荐");
//                } else {
//                    StatService.onEvent(this, "post_comment", "精华", 1);// 统计的点击的次数
//                    MobclickAgentUtils.event(Config.EventId.post_comment, "精华");
//                }
            case R.id.fl_common_bottom:
                StatServiceUtils.event(Config.EventBauduId.answercontent_review, "问答详情页评论");
                replyPost();
                break;
            case R.id.civ_big_thumb://大图
                currentClickedIndex = 0;
                showThumb();
                break;
            case R.id.parise_layout:
                if (!TextUtils.isEmpty(commentId)) {
                    Intent intent1 = new Intent(this, CommentPariseActivity.class);
                    intent1.putExtra(Config.Extra.PARAMETER1, "0");
                    intent1.putExtra(Config.Extra.PARAMETER2, commentId);
                    startActivity(intent1);
                }
                break;

        }


    }


    /**
     * 切换定位
     */
    private void switchItem() {
        if (mListView != null) {
            int current = mListView.getRefreshableView().getFirstVisiblePosition();

            if (current > 0) {
                mListView.getRefreshableView().setSelection(0);
            } else {
                mListView.getRefreshableView().setSelection(2);

            }

        }

    }


    private void share() {
        if (postDetailEntity == null) return;

        if (mShareUtils == null) {
            mShareUtils = new AppShareUtils(this);
        }
        postDetailEntity.setArticleId(postDetailEntity.getICommentId());

        //设置分享类型
        postDetailEntity.setShareType(TypeConstant.SHARE_TYPE_POST);

        if (UserInforManager.getInstance().isLogin()
                && TextUtils.equals(UserInforManager.getInstance().getUserInfor().getUid(), postDetailEntity.getUid())) {
            mShareUtils.showShareDialog(postDetailEntity, TypeConstant.PAGE_MINE_POST);
        } else {
            mShareUtils.showShareDialog(postDetailEntity, TypeConstant.PAGE_POST);
        }
        mShareUtils.setHandleListener(new HandleListener() {
            @Override
            public void handleStart(ArrayList<Object> parms) {

            }

            @Override
            public void handleFail(ArrayList<Object> parms) {

            }

            @Override
            public void handleSuccess(ArrayList<Object> parms) {

            }

            @Override
            public void changeView(ArrayList<Object> parms) {
                mShareUtils.cancleDialog();
                tooleReadMode();
                //通知主页面改变UI
                ArrayList<Object> parms1 = new ArrayList<>();
                parms1.add(TypeConstant.HANDLER_TYPE_1);
                parms1.add(false);//不再改变资源配置
                EventBus.getDefault().post(parms1);
            }
        });

        mShareUtils.setOnDoMoreClickListener(this);
    }


    /**
     * 改变阅读模式
     */
    public void tooleReadMode() {
        mShareUtils = null;
//        reportDialog = null;
        commentListener = null;
        mListView = null;
        mAdapter = null;
        mLoadView = null;
        addCommentIndex = 0;

        setContentView(R.layout.activity_postdetail);
        StatusBarUtils.setupStatusBarView(this, R.color.common_bg_color_2, R.color.common_bg_color_10);
        ViewCompat.requestApplyInsets(findViewById(R.id.container));
        StatusBarUtils.setStatusBarTextColor(this, true);
        initView();
        loadData(true);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (data != null) {
                UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (requestCode == Config.ResultCode.REQUESTCODE_1 && resultCode == RESULT_OK && data != null) {
            if (mAdapter != null) {
                NormalComment curentCommentEntity = ((NormalCommentAdapter) mAdapter).getCurentCommentEntity();
                if (curentCommentEntity != null) {
                    if (data.getBooleanExtra(Config.Extra.PARAMETER1, false)) {
                        curentCommentEntity.setPraiseNum((StringUtils.toInt(curentCommentEntity.getPraiseNum(), 0) + 1) + "");
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }


    }

    /**
     * 回复当前帖子
     */
    private void replyPost() {
        if (postDetailEntity == null) return;
        if (commentListener == null) {
            commentListener = new SubmitCommentListener() {
                @Override
                public void startSubmit() {
                    StatServiceUtils.event(Config.EventBauduId.answercontent_publish, "问答详情页发表");
                }

                @Override
                public void errorSubmit(VolleyError error) {

                }

                @Override
                public void successSubmit(NormalComment normalComment) {
                    StatServiceUtils.event(Config.EventBauduId.answercontent_publishsucces, "问答详情页发表成功");
                    if (normalComment != null) {
                        if (dataList != null) {
                            if (dataList.size() == 1 && dataList.get(0) == null) {
                                dataList.clear();
                                NormalComment normalCommentTitle = new NormalComment();
                                normalCommentTitle.setCommentTitle(AppApplication.getInstance().getString(R.string.speak_title_new));
                                dataList.add(normalCommentTitle);
                                addCommentIndex = 1;
                            }
                            if (dataList.size() > 0 && noNormal && addCommentIndex > 1) {
                                NormalComment normalCommentTitle = new NormalComment();
                                normalCommentTitle.setCommentTitle(AppApplication.getInstance().getString(R.string.speak_title_new));
                                dataList.add(normalCommentTitle);
                                noNormal = false;
                            }
                            try {
                                dataList.add(addCommentIndex, normalComment);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (mAdapter == null) {
                                mAdapter = new NormalCommentAdapter(dataList, mActivity, false);
                                if (mAdapter instanceof NormalCommentAdapter) {
                                    ((NormalCommentAdapter) mAdapter).setOnItemOpration(QuestionDetailActivity.this);
                                }
                                if (mListView != null) {
                                    mListView.setAdapter(mAdapter);
                                }
                            } else {
                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                }
            };
        }

        CommentUtils1.getInstance().showDialog(mActivity, mPageName, "0", postDetailEntity, commentListener, false, true);
    }


    @Override
    public void onDeleteClick() {
        SubmitCommentListener deleteComtListener = new SubmitCommentListener() {
            @Override
            public void startSubmit() {
                DialogUtils.getInstance().showProgressDialog(mActivity, AppApplication.getInstance().getString(R.string.common_deleteing));
            }

            @Override
            public void errorSubmit(VolleyError error) {
                DialogUtils.getInstance().dismissProgressDialog();
                ToastUtils.makeText(AppApplication.getInstance().getString(R.string.common_delete_failure));
            }

            @Override
            public void successSubmit(NormalComment normalComment) {
                DialogUtils.getInstance().dismissProgressDialog();
                ToastUtils.makeText(AppApplication.getInstance().getString(R.string.common_delete_suc));
                if (bundle == null) {
                    bundle = new Bundle();
                }
                bundle.putBoolean("delete", true);

                LiveEventBus.get(Config.EventBus.questionDel).post(true);
                onBackPressed();
            }
        };
        CommentUtils1.getInstance().showDeleteDialog(this, postDetailEntity, deleteComtListener, true);
    }

    @Override
    public void onDefriendClick() {

    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        dataPage = 1;
        addCommentIndex = 0;
        loadData(true);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        dataPage++;
        loadData(false);
    }

    /**
     * LoadViewd 方法
     *
     * @param code 传入对应的值
     * @param msg  提示的的文字
     */
    protected void setLoadView(int code, String msg) {
        switch (code) {
            case NO_NET://网络错误
                if (dataList == null || dataList.size() == 0) {
                    mLoadView.showErrorPage(msg);
                }
                if (mListView != null) {
                    mListView.onRefreshComplete();
                }
                break;
            case DATAERROR://数据错误
                if (dataList == null || dataList.size() == 0) {
                    mLoadView.showErrorPage(msg);
                }
                if (mListView != null) {
                    mListView.onRefreshComplete();
                }
                break;
            case LOADING://加载中
                if (dataList == null || dataList.size() == 0) {
                    mLoadView.showLoadPage(msg);
                }
                break;
            case LOADSUCES://加载成功
                if (dataList == null || dataList.size() == 0) {
                    mLoadView.showSuccess();

                }
                if (mListView != null) {
                    //mListView.onRefreshComplete();
                    mListView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mListView.onRefreshComplete();
                        }
                    }, 500);
                }
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (imgList != null && imgList.size() - 1 >= position) {
            Intent intent = new Intent(this, ImageShowActivity.class);
            intent.putStringArrayListExtra(Config.Extra.PARAMETER1, (ArrayList<String>) imgList);
            intent.putExtra(Config.Extra.PARAMETER2, position);
            intent.setClass(this, ImageShowActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            overridePendingTransition(R.anim.base_alpha_in, R.anim.base_slide_remain);// 前一个参数是下一个Activity的效果
        }
    }


    @Override
    public void onItemDeleted() {
    }
}
